package loader.global;

import java.util.Stack;

import javax.xml.parsers.ParserConfigurationException;

import loader.LoaderXml;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import utils.database.DBPostgre;


public class GlobalXmlLoader extends LoaderXml 
{
	private Stack<GlobalXmlElement> buffer = new Stack<GlobalXmlElement>();
	private int globalOrderId;
	private int documentId;
	//private StringBuffer text = new StringBuffer();

	public GlobalXmlLoader() throws ParserConfigurationException, SAXException {
		super();
		Init();
		// TODO Auto-generated constructor stub
	}

	private void Init()
	{
		DBPostgre.tryTOConnect();
	}

	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.startDocument();
		globalOrderId = 0;
		documentId =  DBPostgre.getValidDocumentId();
		String path = this.getXml_path();
		String nomeArquivo = path.substring(path.lastIndexOf("\\")+1);
		
		DBPostgre.addDocumentToDB(documentId, nomeArquivo);
	}
	
	public void endDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.endDocument();
		DBPostgre.executeAddTextToDB();
		DBPostgre.executeAddAtributeToDB();
		DBPostgre.executeAddElementToDB();
		DBPostgre.executeAddPathToDB();
	}
	
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		
		super.startElement(uri, localName, qName, attributes);
		int attributesNum = attributes.getLength();
		int i = 0;

		GlobalXmlElement element = new GlobalXmlElement();
		globalOrderId++;
		element.setTagName(qName);
		element.setOrderId(globalOrderId);

		buffer.push(element);
		//Vendo se o elemento tem Atributos, se tiver adiciona eles ao banco de dados
		//para cada elemento incrementamos o globalOrderId
		if(attributesNum != 0){
			int parentId = globalOrderId;
			while(i < attributesNum) {
				globalOrderId++;
			    DBPostgre.addAtributeToDB(documentId, globalOrderId, parentId, attributes.getQName(i), attributes.getValue(i));
				i++;
			}
		}
		//text.delete(0, text.length());
	}

	@Override
	public void endElement(String uri, String localName, String qName)
	throws SAXException {
		// TODO Auto-generated method stub
		super.endElement(uri, localName, qName);
		String textValue;
		
		GlobalXmlElement element = buffer.pop();
		if(!buffer.empty()){
			element.setParentId(buffer.lastElement().getOrderId());
		}else element.setParentId(1);
		element.setEnd_desc_id(globalOrderId);
		int auxPathId = lastPathIdCreated;
		element.setPathId(getPathId(current_path_node.toString()));
		if(auxPathId != lastPathIdCreated){
			DBPostgre.addPathToDB(documentId, element.getPathId(), current_path_node.toString());
		}
		//aqui adicionamos o elemento ao banco de dados
		//se o elemento tiver Text tambem adicina aqui
		
		DBPostgre.addElementToDB(documentId, element.getOrderId(), element.getParentId(), element.getTagName(), element.getEnd_desc_id(), element.getPathId());
		textValue = text_atual.toString().trim();
		if(textValue.length() > 0){
			DBPostgre.addTextToDB(documentId, element.getOrderId(), textValue);
		}
		text_atual.delete(0, text_atual.length());
	}
}
